package com.zoro.dyformservice.support.filter.auth;

import com.zoro.dyformservice.support.DyFormSystemProperties;
import com.zoro.dyformservice.support.FilterOrder;
import com.zoro.dyformservice.support.filter.auth.url.AuthUrlSettingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

public class FilterConfig implements WebMvcConfigurer {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    private final DyFormSystemProperties dyFormSystemProperties;

    public FilterConfig(DyFormSystemProperties dyFormSystemProperties) {
        this.dyFormSystemProperties = dyFormSystemProperties;
    }

    @Bean
    public FilterRegistrationBean<LoginFilter> registration(AuthUrlSettingService authUrlSettingService) {
        logger.info("LoginFilter loaded .. ");

        FilterRegistrationBean<LoginFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new LoginFilter(dyFormSystemProperties.getDomain(), dyFormSystemProperties.getUri(),
                dyFormSystemProperties.getIgnoreUri(), authUrlSettingService));
        registration.addUrlPatterns("/*");
        registration.setOrder(FilterOrder.AUTH_FILTER);
        return registration;
    }
}
